package com.oceansoft.mobile.weixin.util;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

@Component
public class MenuUtil {
	
	
	/**
	 * 创建菜单
	 * @param jsonMenu
	 * 返回true，创建成功，返回false，创建失败
	 * @return
	 */
	public static boolean createMenu(Object menu,String token) {
		String jsonMenu = JSONObject.fromObject(menu).toString();
		//创建菜单url
		String url = WeixinUtil.CREATEMENU_URL;
		//菜单创建结果，0标示创建成功
		int result = 0;
		// 拼装创建菜单的url  
		url = url.replace("ACCESS_TOKEN", token);
		System.out.println("拼装创建菜单的url" + url);
		// 调用接口创建菜单  
		JSONObject jsonObject = WeixinUtil.httpsRequest(url, "POST", jsonMenu);
		if (null != jsonObject) {
			if ( 0!= jsonObject.getInt("errcode")) {
				result = jsonObject.getInt("errcode");
				System.out.println("=====errorcode"+result);
			}
		}
		if (result == 0) {
			System.out.println("=====创建菜单成功");
			return true;
		} else {
			System.out.println("=====创建菜单失败");
			return false;
		}
	}
	
	/**
	 * 查询菜单
	 * @param accessToken
	 * @return
	 */
	public static String getMenu(String token){
		String result=null;
		//获取access_token
		String requestUrl=WeixinUtil.GET_URL.replace("ACCESS_TOKEN", token);
		JSONObject jo=WeixinUtil.httpsRequest(requestUrl, "GET", null);
		if(null!=jo){
			result=jo.toString();
			System.out.println("=查询菜单="+result);
		}
		return result;
	}
	
	public static boolean deleteMenu(String token){
		boolean result=false;
		String requestUrl=WeixinUtil.GET_URL.replace("ACCESS_TOKEN", token); 
		JSONObject jo=WeixinUtil.httpsRequest(requestUrl, "GET", null);
		if(null!=jo){
			int errorCode=jo.getInt("errcode");
			if(0==errorCode){
				result=true;
			}else{
				result=false;
			}
		}
		return result;
	}

}
